package com.bill99.schedule;

import com.bill99.service.IpoTrackerWorkOrderDelayService;
import com.bill99.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/***
 * class_name: CalculateDelayHourTask
 * package: com.bill99.report.task
 * creat_user:JunLee
 * creat_date: 2018/7/24
 * creat_time: 15:15
 * describe: 定时分析tracker工单预警监控超时时间等数据
 */

@Component
public class CalculateDelayHourTask {


    @Resource(name = "ipoTrackerWorkOrderDelayServiceImpl")
    public IpoTrackerWorkOrderDelayService service;

    public static final String DATE_PATTERN="yyyy-MM-dd HH:mm:ss";
    public static final String START_DATE_TIME="2018-03-01 00:00:00";

    private static final  Logger LOGGER = LoggerFactory.getLogger(CalculateDelayHourTask.class);

    /***
     * describe: execute
     * creat_time: 2018/7/24 15:48
     * param: []
     * return: void
     */
    @Scheduled(cron = "0 30 17 * * ?")
    public void executeInternal(){

        LOGGER.info("CalculateDelayHourTask execute start.");
        Map<String,Object> workOrder=new HashMap();
        try {
                workOrder.put("beginDate", DateUtil.parse(DATE_PATTERN, START_DATE_TIME));
                workOrder.put("endDate", getYesterdayDate(new Date()));
                //拉取数据到本地
                service.pullData(workOrder);
                LOGGER.info("CalculateDelayHourTask pullData success.");
                //将SLA约束的关系维护到表中
                service.slaRelation();
                LOGGER.info("CalculateDelayHourTask slaRelation success.");
                //分析超时时间 俩种情况
                //SLA约束为T+0
                service.slaTplusZero();
                LOGGER.info("CalculateDelayHourTask slaTplusZero success.");
                //SLA约束不为T+0
                service.slaNotTplusZero();
                LOGGER.info("CalculateDelayHourTask slaNotTplusZero success.");

        } catch (Exception e) {

            LOGGER.error("CalculateDelayHourTask | execute | Exception",e);
            throw new RuntimeException();
        }finally {
            LOGGER.info("CalculateDelayHourTask execute end.");
        }
    }

    private Date getYesterdayDate(Date createTime) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(createTime);
        cal.add(Calendar.DATE,  - 1);
        String formatDateTime = DateUtil.formatDateTime("yyyy-MM-dd", cal.getTime());
        return DateUtil.parse(DATE_PATTERN,formatDateTime+" 17:00:00");
    }


}
